import 'package:flutter_admin/models/odoo/stores_requisition.dart';

class StoresRequisitionInfo {
  String? id;
  String? parentId; // 领料单ID
  String? number; // 领料单号
  String? productId; //物料ID
  String? productCode; // 物料编码
  String? productName; // 物理名称
  String? productDes; // 规格型号
  num? quantity; // 数量
  String? productUomName; // 单位
  StoresRequisition? storesRequisition;

  StoresRequisitionInfo({
    this.id,
    this.parentId,
    this.number,
    this.productId,
    this.productCode,
    this.productName,
    this.productDes,
    this.quantity,
    this.productUomName,
    this.storesRequisition
  });

  @override
  String toString() {
    return '''StoresRequisitionInfo{
      id: $id,
      number: $number,
      parentId: $parentId,
      productId: $productId,
      productCode: $productCode,
      productName: $productName,
      productDes: $productDes,
      quantity: $quantity,
      productUomName: $productUomName,
    }''';
  }

  factory StoresRequisitionInfo.fromMap(Map<String, dynamic> map) {
    return new StoresRequisitionInfo(
      // id: map['id'] != false ? map['id'].toString() as String? : "",
      productCode: map['default_code'] != false
          ? map['default_code'].toString() as String?
          : "",
      productId: map['product_tmpl_id'] != false
          ? map['product_tmpl_id'][0].toString() as String?
          : "",
      productName: map['product_name'] != false
          ? map['product_name'].toString() as String?
          : "",
      productDes: map['description'] != false
          ? map['description'].toString() as String?
          : "",
      quantity: map['quantity'] != false ? map['quantity'] : 0,
      productUomName: map['product_uom_id'] != false
          ? map['product_uom_id'][1].toString() as String?
          : "",
    );
  }
  factory StoresRequisitionInfo.fromMapDetail(Map<String, dynamic> map,{StoresRequisition? storesRequisition}) {
    return new StoresRequisitionInfo(
      id: map['id'] != false ? map['id'].toString() as String? : "",
      productCode: map['product_tmpl_id']['default_code'] != false
          ? map['product_tmpl_id']['default_code'].toString() as String?
          : "",
      productId: map['product_tmpl_id'] != false
          ? map['product_tmpl_id']['id'].toString() as String?
          : "",
      productName: map['product_tmpl_id']['name'] != false
          ? map['product_tmpl_id']['name'].toString() as String?
          : "",
      productDes: map['product_tmpl_id']['description'] != false
          ? map['product_tmpl_id']['description'].toString() as String?
          : "",
      quantity: map['quantity'] != false ? map['quantity'] : 0,
      productUomName: map['product_tmpl_id'] != false
          ? map['product_tmpl_id']['uom_id'][1].toString() as String?
          : "",
      storesRequisition: storesRequisition,
    );
  }
  Map<String, dynamic> toMap() {
    // ignore: unnecessary_cast
    Map<String, dynamic> map = {
      'id': this.id,
      'parentId': this.parentId,
      'productCode': this.productCode,
      'productName': this.productName,
      'productDes': this.productDes,
      'quantity': this.quantity,
      'productUomName': this.productUomName,
    };
    if(this.storesRequisition != null){
      map.addAll({
        'number': this.storesRequisition!.number,
        'purpose': this.storesRequisition!.purpose,
        'workshopName': this.storesRequisition!.workshopName,
        'userName': this.storesRequisition!.userName,
        'requisitionDate': this.storesRequisition!.requisitionDate,
      });
    }
    return map;
  }

  List toDomainList() {
    // ignore: unnecessary_cast
    List list = [
      // if (this.workshopId != null && this.workshopId != '')
      //   ['workshop_id', '=', int.parse(this.workshopId!)],
      if (this.productName != null && this.productName != '')
        ['name', 'like', "%${this.productName}%"],
    ] as List;
    return list;
  }

  List toDetailDomainList() {
    // ignore: unnecessary_cast
    List list = [
      if (this.parentId != null && this.parentId != '')
        ['parent_id', '=', int.parse(this.parentId!)],
    ] as List;
    return list;
  }

  Map<String, dynamic> toOdooMap() {
    // ignore: unnecessary_cast
    return {
      'parent_id': int.parse(this.parentId!),
      'product_tmpl_id': int.parse(this.productId!),
      'quantity': this.quantity,
    } as Map<String, dynamic>;
  }
}
